/*
 * DateHelper.java
 *
 * Created on April 24, 2007, 5:08 PM
 *
 *  Life Logger v1.0
    Copyright (C) 2007  Anand Kishore (anand@semanticvoid.com)
 
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.
 
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License along
    with this program; if not, write to the Free Software Foundation, Inc.,
    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

package com.semanticvoid.lifelogger.utils;

import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

/**
 * The helper class for date related functions.
 * @author anand kishore
 */
public class DateHelper {
    
    /**
     * Function to convert the calendar object into a mysql date string
     * @param date the calendar object
     * @return the mysql string representation
     */
    public static String parse(Calendar date) {
        String mysqlDate;
        
        mysqlDate = "" + date.get(date.YEAR) +  "-" + format((date.get(date.MONTH)+1)) + "-" + format(date.get(date.DATE)) + " " + format(date.get(date.HOUR_OF_DAY)) + ":" + format(date.get(date.MINUTE)) + ":" + format(date.get(date.SECOND));
        
        return mysqlDate;
    }
    
    
    /**
     * Make single digits a double digit string
     * @param value the single digit
     * @return the padded string
     */
    public static String format(int value) {
        
        String str = String.valueOf(value);
        
        if(str.length() <= 1) {
            return "0" + str;
        } else {
            return str;
        }
    }
    
    /**
     * Convert the delicious date string to a date object
     * @param dTime the date string
     * @return the calendar object
     */
    public static Calendar deliciousTimeToDate(String dTime) {
        // Holds the date string at index 0 and the time string at index 1
        String[] dateTime = new String[2];
        
        String[] temp = dTime.split("T");
        dateTime[0] = temp[0];
        
        dateTime[1] = temp[1].split("Z")[0];
        
        // Holds the date tokens and time tokens
        String[] date = dateTime[0].split("-");
        String[] time = dateTime[1].split(":");
        
        Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
        // Set the appropriate tokens from above
        cal.set(Integer.valueOf(date[0]), Integer.valueOf(date[1]) - 1, Integer.valueOf(date[2]), Integer.valueOf(time[0]), Integer.valueOf(time[1]), Integer.valueOf(time[2]));
        
        return cal;
    }


    public static Calendar convertMySQLDateStringToCalendar(String dateString) {
        if(dateString == null) {
            return null;
        }
        
        int year;
        int month;
        int date;
        int hour;
        int minute;
        int second;
        
        String[] dateTimeTokens = dateString.split(" ");
        
        // Form date tokens
        if(dateTimeTokens[0] != null && !dateTimeTokens[0].equals("")) {
            String[] dateTokens = dateTimeTokens[0].split("-");
            
            year = Integer.valueOf(dateTokens[0]);
            month = Integer.valueOf(dateTokens[1]) - 1;
            date = Integer.valueOf(dateTokens[2]);
        } else {
            return null;
        }
        
        
        // Form time tokens
        if(dateTimeTokens[1] != null && !dateTimeTokens[1].equals("")) {
            String[] timeTokens = dateTimeTokens[1].split(":");
            
            hour = Integer.valueOf(timeTokens[0]);
            minute = Integer.valueOf(timeTokens[1]);
            //second = Integer.valueOf(timeTokens[2]);
            second = 0;
        } else {
            return null;
        }
        
        Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
        cal.set(year, month, date, hour, minute, second);
        
        return cal;
    }
}
